/**
 * Copyright (C) 2017 - 2020 https://github.com/joewee
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.bifrost.game.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonManagedReference;

import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * 抽奖游戏实体类
 *
 * @author joewee
 * @version 1.0.0
 * @Date 2018/5/8 11:09
 */
@Entity
@Table(name = "LOTTERY_GAME",indexes = {
        //@Index(name = "UID_INDEX",columnList = "UID",unique = true)
})
public class LotteryGame implements Serializable {
    public static final String LOCK_KEY_PERFIX="com.bifrost.lotteryGame";

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "ID", unique = true, nullable = false)
    private Long id;

    @Column(name = "UID", nullable = false,length = 36)
    private String uid = UUID.randomUUID().toString();
    /**
     * 游戏名称
     */
    @Column(name = "NAME")
    private String name;
    /**
     * 游戏类型
     */
    @Column(name = "GAME_TYPE", length = 32)
    @Enumerated(value = EnumType.STRING)
    private Type gameType = Type.LUCKY_WHEEL;
   /* *//**
     * 状态
     *//*
    @Column(name = "STATUS", length = 32)
    @Enumerated(value = EnumType.STRING)
    private Status status = Status.ENABLE;*/
    @Column(name = "IS_ENABLED",nullable = false)
    @JsonIgnore
    private Boolean enabled =Boolean.TRUE;
    /**
     * 开始时间
     */
    @Column(name = "START_TIME")
    @Temporal(value = TemporalType.TIMESTAMP)
    private Date startTime = new Date();
    ;
    /**
     * 结束时间
     */
    @Column(name = "END_TIME")
    @Temporal(value = TemporalType.TIMESTAMP)
    private Date endTime = new Date();

    /**
     * 每人每天参与次数
     */
    @Column(name = "DAY_ATTEND_LIMIT", length = 11, scale = 2)
    private Integer dayAttendLimit=0;
    /**
     * 每人参与总共次数
     */
    @Column(name = "ATTEND_COUNT_LIMIT", length = 11, scale = 2)
    private Integer attendCountLimit=0;
    /**
     * 每人每天中奖次数限制
     */
    @Column(name = "DAY_WIN_LIMIT", length = 11, scale = 2)
    private Integer dayWinLimit=0;
    /**
     * 每人中奖总共次数限制
     */
    @Column(name = "WIN_COUNT_LIMIT", length = 11, scale = 2)
    private Integer winCountLimit=1;
    /**
     * 游戏中奖总次数限制
     */
    @Column(name = "WINNERS_COUNT_LIMIT", length = 11, scale = 2)
    private Integer winnersCountLimit=0;

    /**
     * 关注链接
     */
    @Column(name = "URL")
    private String url;

    /**
     * 游戏描述
     */
    @Column(name = "DESCRIPTION")
    private String description;

    /**
     * 关键字
     */
    @Column(name = "KEYWORD", length = 32)
    private String keyword;

    /**
     * 参与次数
     */
    @Column(name = "ATTEND_COUNT", length = 11, scale = 2)
    private Integer attendCount = 0;
    /**
     * 游戏所属门店
     */
    @Column(name = "MERCHANT_ID")
    private Long merchantId;

    /**
     * 创建时间
     */
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "CREATED_TIMESTAMP")
    private Date createdTimestamp = new Date();

    @OneToMany(mappedBy = "lotteryGame",fetch = FetchType.LAZY)
    @JsonManagedReference
    private List<LotteryGameAward> lotteryGameAwards ;
    @OneToMany(mappedBy = "lotteryGame",fetch = FetchType.LAZY)
    @JsonIgnore
    private List<LotteryGameWinner> lotteryGameWinners ;

    @Deprecated
    public enum Status {
        ENABLE, DISABLE;
    }

    public enum Type {
        LUCKY_WHEEL
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getUid() {
        return uid;
    }

    public void setUid(String uid) {
        this.uid = uid;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Type getGameType() {
        return gameType;
    }

    public void setGameType(Type gameType) {
        this.gameType = gameType;
    }

    public Boolean isEnabled() {
        return enabled;
    }

    public void setEnabled(Boolean enabled) {
        this.enabled = enabled;
    }

    public Date getStartTime() {
        return startTime;
    }

    public void setStartTime(Date startTime) {
        this.startTime = startTime;
    }

    public Date getEndTime() {
        return endTime;
    }

    public void setEndTime(Date endTime) {
        this.endTime = endTime;
    }

    public Integer getDayAttendLimit() {
        return dayAttendLimit;
    }

    public void setDayAttendLimit(Integer dayAttendLimit) {
        this.dayAttendLimit = dayAttendLimit;
    }

    public Integer getAttendCountLimit() {
        return attendCountLimit;
    }

    public void setAttendCountLimit(Integer attendCountLimit) {
        this.attendCountLimit = attendCountLimit;
    }

    public Integer getDayWinLimit() {
        return dayWinLimit;
    }

    public void setDayWinLimit(Integer dayWinLimit) {
        this.dayWinLimit = dayWinLimit;
    }

    public Integer getWinCountLimit() {
        return winCountLimit;
    }

    public void setWinCountLimit(Integer winCountLimit) {
        this.winCountLimit = winCountLimit;
    }

    public Integer getWinnersCountLimit() {
        return winnersCountLimit;
    }

    public void setWinnersCountLimit(Integer winnersCountLimit) {
        this.winnersCountLimit = winnersCountLimit;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getKeyword() {
        return keyword;
    }

    public void setKeyword(String keyword) {
        this.keyword = keyword;
    }

    public Integer getAttendCount() {
        return attendCount;
    }

    public void setAttendCount(Integer attendCount) {
        this.attendCount = attendCount;
    }

    public Long getMerchantId() {
        return merchantId;
    }

    public void setMerchantId(Long merchantId) {
        this.merchantId = merchantId;
    }

    public Date getCreatedTimestamp() {
        return createdTimestamp;
    }

    public void setCreatedTimestamp(Date createdTimestamp) {
        this.createdTimestamp = createdTimestamp;
    }

    public boolean isActive() {
        return Boolean.TRUE.equals(isEnabled())&&System.currentTimeMillis() >= this.getStartTime().getTime() && System.currentTimeMillis() <= this.getEndTime().getTime();
    }

    public List<LotteryGameAward> getLotteryGameAwards() {
        return lotteryGameAwards;
    }

    public void setLotteryGameAwards(List<LotteryGameAward> lotteryGameAwards) {
        this.lotteryGameAwards = lotteryGameAwards;
    }

    public List<LotteryGameWinner> getLotteryGameWinners() {
        return lotteryGameWinners;
    }

    public void setLotteryGameWinners(List<LotteryGameWinner> lotteryGameWinners) {
        this.lotteryGameWinners = lotteryGameWinners;
    }

}
